package test.src.awt_swing;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JProgressBarDemo2 {
    JFrame jFrame = new JFrame("这里测试进度条");

    JProgressBar jProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);

    JCheckBox indeterminate = new JCheckBox("不确定进度");
    JCheckBox noBorder = new JCheckBox("不绘制边框");

    private void init() {
        //组装视图
        Box vBox = Box.createVerticalBox();
        vBox.add(indeterminate);
        vBox.add(noBorder);

        Box content = Box.createHorizontalBox();

        content.add(vBox);
        content.add(jProgressBar);

        jFrame.add(content);

        //开启耗时任务
        SimulatedActivity simulatedActivity = new SimulatedActivity(100);
        new Thread(simulatedActivity).start();

        jProgressBar.setMinimum(0);
        jProgressBar.setMaximum(100);

        jProgressBar.setBorderPainted(true);
        jProgressBar.setStringPainted(true);
        jProgressBar.setIndeterminate(false);

        indeterminate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean selected = indeterminate.isSelected();
                jProgressBar.setIndeterminate(selected);
                jProgressBar.setStringPainted(!selected);
            }
        });

        noBorder.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean selected = noBorder.isSelected();
                jProgressBar.setBorderPainted(!selected);
            }
        });

        jFrame.pack();
        jFrame.setVisible(true);

        //通过定时器，不断的读取simulatedActivity中的current值，更新进度条的进度
        Timer timer = new Timer(300, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jProgressBar.setValue(simulatedActivity.getCurrent());
            }
        });

        timer.start();

        jProgressBar.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (jProgressBar.getValue() == jProgressBar.getMaximum()) {
                    timer.stop();
                }
            }
        });
    }

    public static void main(String[] args) {
        new JProgressBarDemo2().init();
    }


}

//定义一个线程任务，模拟耗时操作
class SimulatedActivity implements Runnable {

    //被volatile修饰的变量，如果值发生了变化，其他线程立马可见
    private volatile int current = 0;
    private int amount;

    public SimulatedActivity(int amount) {
        this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public int getCurrent() {
        return current;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    @Override
    public void run() {
        while (current < amount) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            current++;
        }
    }
}
